let rs=new Vue({
   el:'.page-content',
    data:function () {
            return {
                role:parent.layer.role,
                treeObj:{},
                officeTreeObj:{},
                setting:{
                    check: {
                        enable: true,
                        chkboxType: {"Y": "ps", "N": "ps"}  //"Y"设置选中是否同时级联控制父子级节点选中状态  "p"父节点级联选中  "s"子节点级联选中
                    },
                    data:{//设置树的数据的配置
                        simpleData: {//简单数据格式配置  一维结构节点数组 不设置默认是多维结构数组
                            enable:true,  //开启使用简单数据格式
                            // idKey: 'id', //设置节点中的id属性key  默认
                            pIdKey: 'parentId' //设置父节点id属性key   与数据库中的字段需要对应
                        },
                    },
                    callback:{
                        onClick:this.onClick2
                    },
                    view:{//显示设置
                        //当显示某个节点/更新节点的时候，自动会调用字体设置
                        fontCss: this.fontCss1
                    }
                },
                nodes:[],
                officeNodes:[],
                upRole:{
                    role:parent.layer.role,
                    resourcesId:[],
                    newResourcesId:[],
                    officeId:[],
                    newOfficeId:[]
                }
        }
    },
    methods:{
        onClick2:function (event, treeId, treeNode) {

        },
        initTree:function () {
            axios({
                url:'resource/resource'
            }).then(response=>{
                this.nodes=response.data.obj;
                //动态生成父节点 全部
                this.nodes[this.nodes.length] = {id:0,name:'权限列表'};
                //初始化树(dom节点对象,配置对象,节点数组)
                this.treeObj = $.fn.zTree.init($('#select-treetreeSelectResEdit'),this.setting,this.nodes);
                this.treeObj.expandAll(true);
                this.selectResourceByRid();
            }).catch(error=>{
                layer.msg(error.message)
            })
        },
        selectResourceByRid:function(){
            axios({
                url:'resource/selectResourceByRid',
                params:{id:this.role.id}
            }).then(response=>{
                let chose = response.data.obj;
                let _nodes=this.treeObj.transformToArray(this.treeObj.getNodes());

                for (let i = 0; i <chose.length ; i++ ) {
                    for (let j = 0; j <_nodes.length ; j++ ) {
                        if (chose[i].id === _nodes[j].id) {
                            _nodes[j].checked=true;
                            this.treeObj.updateNode(_nodes[j]);
                            this.upRole.resourcesId.push(_nodes[j].id)
                        }
                    }
                }
            })
        },
        selectOfficeByRid:function(){
            axios({
                url:'role/selectOfficeByRid',
                params:{rid:this.role.id}
            }).then(response=>{
                this.upRole.officeId = response.data;
                let _nodes=this.treeObj.transformToArray(this.officeTreeObj.getNodes());
                for (let i = 0; i < this.upRole.officeId.length; i++) {
                    for (let j = 0; j <_nodes.length; j++) {
                        if (_nodes[j].id === this.upRole.officeId[i]) {
                            _nodes[j].checked=true;
                            this.officeTreeObj.updateNode(_nodes[j]);
                        }
                    }
                }
            })
        },
        dataScopeTree:function(){
            if (this.role.dataScope==='9'){
                this.initOfficeTree();
                $("#treeSelectOfficeEdit").css("display","inline-block")
            }else {
                $.fn.zTree.destroy("select-treetreeSelectOfficeEdit");
                //隐藏div
                $("#treeSelectOfficeEdit").css("display","none");
                this.officeTreeObj={};
            }
        },
        initOfficeTree:function () {
            axios({
                url:'office/select'
            }).then(response=>{
                this.officeNodes=response.data.obj;
                //动态生成父节点 全部
                this.officeNodes[this.officeNodes.length] = {id:0,name:'机构列表'};
                //初始化树(dom节点对象,配置对象,节点数组)
                this.officeTreeObj = $.fn.zTree.init($('#select-treetreeSelectOfficeEdit'),this.setting,this.officeNodes);
                this.officeTreeObj.expandAll(true);
                this.selectOfficeByRid()
            }).catch(error=>{
                layer.msg(error.message)
            })
        },
        updateResourceAndOffice:function(){
            let _resources = this.treeObj.getCheckedNodes(true);
            for (let i = 0; i <_resources.length ; i++) {
                if (_resources[i].id===0){
                    continue
                }
                this.upRole.newResourcesId.push(_resources[i].id)
            }
            if (this.role.dataScope === '9') {
                let _office = this.officeTreeObj.getCheckedNodes(true);
                for (let i = 0; i <_office.length ; i++) {
                    if (_office[i].id===0){
                        continue
                    }
                    this.upRole.newOfficeId.push(_office[i].id)
                }
            }
            this.upRole.role = JSON.stringify(this.role);
            axios({
                url:'role/updateResourceAndOffice',
                method:'put',
                data:this.upRole
            }).then(response=> {
                if(response.data>0){
                    let index=parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.layer.message=response.data.obj;

                }else {
                    layer.msg(response.data.msg)
                }
            })
        },
        hideWindow:function () {
            let index=parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    },
    created:function () {
        this.initTree();
        this.dataScopeTree()
    }
});